import {
	baseUrl,
	encryptDes,
	decryptDes
} from './util.js'
import qs from 'qs'

export default function request({
	url,
	method,
	param,
	producttype,
	dataType,
	responseType,
	timeout,
	checkLogin,
}) {
	var token = uni.getStorageSync("token")
	if (token) {
		var header = {
			'Authorization': token,
		}
	} else {
		if(checkLogin===false){
			var header = {}
		}else{
			uni.redirectTo({url:'/pages/login/login'})
			return false
		}
	}
	return new Promise((resolve, reject) => {
		var queryParams=qs.stringify({
			param: encryptDes(JSON.stringify({
				parampd: param || '',
				producttype: producttype==1?'ring':producttype==2?'qwvedio':producttype
			})),
			login_name: 'xnhsmallpro'
		});
		uni.request({
			url: baseUrl + url+'?'+queryParams,
			method: method || 'post',
			header: header,
			dataType: dataType,
			responseType: responseType,
			timeout: timeout,
		}).then(response=>{
			var res=response[1]
			if(res.statusCode == 200){
				var data = res.data
				if (data.code == "-10000" || data.code == "-10001") {
					uni.redirectTo({url:'/pages/login/login',success:()=>{console.log(777)}})
					return
				}
				if (data.code == "-100009") {
					uni.showToast({
						title: '没有访问权限',
						icon: "error",
						duration: 2000,
					});
					return
				}
				if(data){
					resolve(data)
				}
			}else {
				uni.showToast({
					title: response.errMsg,
					icon: "error",
					duration: 2000,
				});
				reject(response.errMsg)
			}
		}).catch(err=>{
			reject()
		})
	})
}
